package com.wl.api.controller.system;

import java.util.function.Predicate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.wl.api.common.utils.BeanUtils;
import com.wl.api.common.utils.ErrorCodeEnum;
import com.wl.api.common.utils.JsonObject;
import com.wl.api.common.utils.ObjectUtil;
import com.wl.api.common.utils.StringUtil;
import com.wl.api.controller.base.CommonController;
import com.wl.api.dto.base.CommonUserDTO;
import com.wl.api.dto.system.OmsIndexInformationDTO;
import com.wl.api.dto.system.OmsProductServiceDTO;
import com.wl.api.model.system.OmsIndexInformation;
import com.wl.api.model.system.OmsProductService;
import com.wl.api.service.business.IOmsProductServiceService;

/**
 * 推荐产品列表后台管理
 * @author Administrator
 *
 */
@RestController
@RequestMapping("/admin/oms/recommend")
public class AdminRecommendController extends CommonController {
    
	@Autowired
	IOmsProductServiceService productService;
	
	@RequestMapping(value="/prodSer", method=RequestMethod.POST)
	public JsonObject createProdService(@RequestBody OmsProductServiceDTO param) throws Exception {
		JsonObject json = JsonObject.getInstance();
		boolean pass = true;
		String title = "", code = "";
		if (validate().or(s -> s.getId() != null).test(param)) {
			title = ErrorCodeEnum.param_error.getMessage();
			code = ErrorCodeEnum.param_error.getError_code();
			pass = false;
		}
		if(pass) {
			initCreateUser(param);
			OmsProductService entity = BeanUtils.copyProperties(param, OmsProductService.class);
			productService.insert(entity);
			json.setData(entity.getId());
		}
		setJsonMessage(json, title, code, pass);
		return json;
	}
	/**
	 *  修改产品数据
	 * @param param
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/prodSer", method = RequestMethod.PUT)
	JsonObject modify(@RequestBody OmsProductServiceDTO param) throws Exception {
		JsonObject json = JsonObject.getInstance();
		boolean pass = true;
		String title = "" , error_code = ""; 
		if (validate().and(s -> s.getId() == null).test(param)) {
			title = ErrorCodeEnum.param_error.getMessage();
			error_code = ErrorCodeEnum.param_error.getError_code();
			pass = false;
		}
		//不能修改上架数据
		if(pass && !ObjectUtil.isEmpty(productService.selectOne(new EntityWrapper<OmsProductService>(OmsProductService.builder().status(1).id(param.getId()).build())))) {
			error_code = ErrorCodeEnum.bad_request_published_data.getError_code();
			title = ErrorCodeEnum.bad_request_published_data.getMessage();
			pass = false;
		}
		if(pass) {
			initUpdateUser(param);
			OmsProductService entity = BeanUtils.copyProperties(param, OmsProductService.class);
			productService.updateById(entity);
			json.setData(entity.getId());
		}
		setJsonMessage(json, title, error_code, pass);
		return json;
	}
	/** 只提供修改数据状态 */
	@RequestMapping(value = "/prodSer/{id}", method = RequestMethod.PUT)
	JsonObject modifyStatus(@PathVariable("id")Integer id, Integer status) throws Exception {
		JsonObject json = JsonObject.getInstance();
		productService.updateById(OmsProductService.builder().status(status).updateUser(getCommonUser().getUsername()).id(id).build());
		setJsonMessage(json);
		return json;
	}
	
	@RequestMapping(value="/prodSer/{id}", method = RequestMethod.DELETE)
	JsonObject del(@PathVariable("id") Integer id) throws Exception {
		JsonObject json = JsonObject.getInstance();
		boolean pass = true;
		String title = "" , error_code = ""; 
		
		if(productService.selectOne(new EntityWrapper<OmsProductService>(OmsProductService.builder().status(0).id(id).build())) == null) {
			error_code = ErrorCodeEnum.bad_request_published_data.getError_code();
			title = ErrorCodeEnum.bad_request_published_data.getMessage();
			pass = false;
		}
		if (pass) {
			productService.deleteById(id);
		}
		setJsonMessage(json, title, error_code, pass);
		return json;
	}
	
	@RequestMapping(value ="/prodSer",method = RequestMethod.GET)
	public JsonObject getItemList(OmsProductServiceDTO param) {
		JsonObject json = JsonObject.getInstance();
		param.buildStartTime(param.getStartTime()).buildEndTime(param.getEndTime()).buildPage(param.getPage(), param.getPageSize());
		json.setPage(productService.getItemList(param));
		setJsonMessage(json);
		return json;
	}  
	
	@RequestMapping(value = "/prodSer/{id}", method = RequestMethod.GET)
	public JsonObject getDetail(@PathVariable("id")Integer id) {
		JsonObject json = JsonObject.getInstance();
		json.setData(productService.selectById(id));
		setJsonMessage(json);
		return json;
	}
	
	private void initCreateUser(OmsProductServiceDTO param) throws Exception {
		CommonUserDTO user = getCommonUser();
		param.buildCreateMsg(user.getId());
	}
	private void initUpdateUser(OmsProductServiceDTO param) throws Exception {
		CommonUserDTO user = getCommonUser();
		param.buildUpdateMsg(user.getUsername());
		
	}
	public Predicate<OmsProductServiceDTO> validate() {
		return  s -> StringUtil.isEmpty(s.getImgUrl()) || StringUtil.isEmpty(s.getLinkUrl())
				|| StringUtil.isEmpty(s.getProductDesc()) || StringUtil.isEmail(s.getProductTitle());
	}
}
